#include "ClassEditModel.h"


ClassEditModel::ClassEditModel(QObject *parent) :
    QStandardItemModel(parent)
{
}


void ClassEditModel::setClassModel(D20CharacterModel *model)
{
    if (!m_character.isNull())
    {
        disconnect(m_character.data(), SIGNAL(layoutChanged()), this, SLOT(refresh()));
    }

    m_character = model;
    refresh();

//    connect(m_character.data(), SIGNAL(layoutChanged()), this, SLOT(refresh()));

}


void ClassEditModel::setCampaignModel(D20CampaignModel *model)
{
    if (!m_campaign.isNull())
    {
        disconnect(m_campaign.data(), SIGNAL(layoutChanged()), this, SLOT(refresh()));
    }

    m_campaign = model;
    refresh();

//    connect(m_campaign.data(), SIGNAL(layoutChanged()), this, SLOT(refresh()));
}

bool ClassEditModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (role != Qt::CheckStateRole) return false;

    QModelIndex nameIndex = index.sibling(index.row(), NameField);
    if (!nameIndex.isValid()) return false;

    switch (value.toInt())
    {
    case Qt::Checked:
        m_character->addItem(D20CharacterModel::ClassSkillsGroup, nameIndex.data().toString());
        return true;
    case Qt::Unchecked:
        return true;
    }

    return false;

}


void ClassEditModel::refresh()
{
    clear();
    emit layoutAboutToBeChanged();

    if (m_campaign.isNull() || m_character.isNull()) return;

    // create top level items from character class model:
    for (int group = D20CharacterModel::Group_FIRST; group <= D20CharacterModel::Group_LAST; group++)
    {
        QModelIndex charIndex = m_character->groupIndex(group);
        if (!charIndex.isValid()) continue;

        QList<QStandardItem*> top_items;
        for (int field = Field_FIRST; field <= Field_LAST; field++)
        {
            QStandardItem* item = new QStandardItem();
            if (field == NameField) item->setText(m_character->groupName(group));

            top_items << item;
        }

        if (!top_items.count()) continue;

        insertRow(rowCount(QModelIndex()),top_items);

        switch (group)
        {
        case D20CharacterModel::ClassSkillsGroup:
            break;
        default:
            continue;
        }

        QModelIndex campaignSkillIndex = m_campaign->groupIndex(D20CampaignModel::SkillGroup).child(0, AbstractItem::IdData);
        while (campaignSkillIndex.isValid())
        {
            QList<QStandardItem*> sub_items;
            QModelIndex skillNameIndex = campaignSkillIndex.sibling(campaignSkillIndex.row(), AbstractItem::NameData);

            QString name = skillNameIndex.data(Qt::DisplayRole).toString();
            QStandardItem* item = new QStandardItem(name);
            sub_items << item;

            item = new QStandardItem();
            sub_items << item;
            item->setCheckable(true);

            QModelIndexList classSkills = m_character->match(m_character->groupIndex(group),
                                                             Qt::DisplayRole,
                                                             name,
                                                             1,
                                                             Qt::MatchEndsWith);
            if (!classSkills.isEmpty())
            {
                item->setCheckState(Qt::Checked);
            }

            QStandardItem* top_item = top_items.first();
            top_item->insertRow(top_item->rowCount(), sub_items);

            campaignSkillIndex = campaignSkillIndex.sibling(campaignSkillIndex.row()+1, campaignSkillIndex.column());
        }


    }


    emit layoutChanged();
}
